﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.Practices.Unity;
using Microsoft.Surface.Presentation.Controls;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.UI.View.Util;
using ScrumTable.UI.View.WinTouch.UserControls.Tool;
using ScrumTable.UI.ViewModel;
using ScrumTable.UI.ViewModel.Tools;

namespace ScrumTable.UI.View.WinTouch.Util
{
	/// <summary>
	/// Interaction logic for SearchScreen.xaml
	/// </summary>
    public partial class DynamicErrorControl
    {

        #region Decleration
        //--------------------------------------------------------------------
        // Decleration
        //--------------------------------------------------------------------
        public Dictionary<string, object> SelectedValues { get; set; }
        public Panel BaseControl { get; set; }
        public string FieldName { get; set; }
    
        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        public DynamicErrorControl(Panel panel, IScrumItemViewModel<ScrumItemBase> model, string name)
        {
            FieldName = name;
            BaseControl = panel;
            DataContext = model;
            Resources["Converter"] = new ConverterErrorInfoToErrorIfFieldIsDefined() { FieldName = name };
            Resources["Converter2"] = new ConverterErrorInfoToBorderThicknessIfFieldIsDefined() { FieldName = name };
            InitializeComponent();
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------



        #endregion

        #region Converters for Error
        //--------------------------------------------------------------------
        // Converters for Error
        //--------------------------------------------------------------------
        private class ConverterErrorInfoToErrorIfFieldIsDefined : IValueConverter
        {
            public string FieldName { get; set; }

            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (string.IsNullOrEmpty(FieldName) || value == null)
                {
                    return null;
                }
                else
                {
                    IList<DomainSyncErrorInfo> list = (IList<DomainSyncErrorInfo>)value;
                    var error = list.FirstOrDefault((x) => x.Field != null && x.Field.Name == FieldName);
                    if (error != null)
                    {
                        return Localization.LanguageDictionary.Current.Translate<string>("Error_Field_" + error.FieldErrorReason, "Text");
                    }
                }
                return null;
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }


        private class ConverterErrorInfoToBorderThicknessIfFieldIsDefined : IValueConverter
        {
            public string FieldName { get; set; }

            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                if (string.IsNullOrEmpty(FieldName) || value == null)
                {
                    return 0;
                }
                else
                {
                    IList<DomainSyncErrorInfo> list = (IList<DomainSyncErrorInfo>)value;
                    var error = list.FirstOrDefault((x) => x.Field != null && x.Field.Name == FieldName);
                    if (error != null)
                    {
                        return 2;
                    }
                }
                return 0;
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
        #endregion
    }
}